﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ExplodingCow.Basilisk.Logic;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WPFDesktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ItemList itemList= ItemList.GetItemList();

        public MainWindow()
        {
            InitializeComponent();
          
        }
        
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ItemAdd.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
            ItemAdd.StaysOpen = true;
            ItemAdd.IsOpen = true;
            resetPopup(new Item());
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            int result = 0;
            decimal decResult = 0;
            Item item = new Item()
            {
                Quantity = int.TryParse(txtQuantity.Text, out result) ? int.Parse(txtQuantity.Text) : 0,
                Name = txtProductName.Text,
                Description = txtDescription.Text,
                Category = (Category)cmbCategory.SelectedValue,
                Cost = decimal.TryParse(txtCost.Text, out decResult) ? decimal.Parse(txtCost.Text) : 0,
                StoreLocation = new StoreLocation()
                {
                    Name = txtStoreName.Text,
                    Address = txtAdress.Text,
                    City = txtCity.Text,
                    State = txtState.Text,
                    Zip = txtPostalCode.Text
                }
            };
            itemList.Add(item);
            ItemAdd.IsOpen = false;
            lstItems.DataContext = ItemList.ToObservableCollection<Item>(itemList.Items);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            ItemAdd.IsOpen = false;
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            itemList.Remove((Item)lstItems.SelectedItem);
            lstItems.DataContext = ItemList.ToObservableCollection<Item>(itemList.Items);
        }

        private void MenuFile_MouseEnter(object sender, MouseEventArgs e)
        {
            menuFile.Foreground = Brushes.Gold;
            menuFile.Background = Brushes.Firebrick;
        }

        private void menuFile_MouseLeave(object sender, MouseEventArgs e)
        {
            menuFile.Foreground = Brushes.Firebrick;
            menuFile.Background = Brushes.Gold;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cmbCategory.DataContext = Enum.GetValues(typeof(Category));
        }

        private void menuOpen_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog()
            {
                FileName = "List1",
                DefaultExt = ".Xml",
                Filter = "XML Files (.Xml) |*.Xml"
            };

            Nullable<bool> result = openFile.ShowDialog();

            if (result == true)
            {
                string filename = openFile.FileName;
                ListLoader load = ListLoader.GetListLoader();
                itemList.Items = load.LoadList(filename);
                lstItems.DataContext = ItemList.ToObservableCollection<Item>(itemList.Items);
            }
        }

        private void menuExit_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Do you really want to exit this application?", "Exit", MessageBoxButton.YesNoCancel);
            if (result == MessageBoxResult.Yes)
            {
                Application.Current.MainWindow.Close();
            }
        }

        private void menuSave_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog()
            {
                FileName = "List1",
                DefaultExt = ".Xml",
                Filter = "XML Files (.Xml) |*.Xml"
            };

            Nullable<bool> result = saveFile.ShowDialog();

            if (result == true)
            {
                string filename = saveFile.FileName;
                ListSaver saver = ListSaver.GetListSaver();
                saver.Save(filename,saveFile.SafeFileName, itemList.Items);
            }
        }

        private void menuNew_Click(object sender, RoutedEventArgs e)
        {
            itemList.Items = new List<Item>();
            lstItems.DataContext = ItemList.ToObservableCollection<Item>(itemList.Items);
        }

        private void lstItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Item item = (Item)lstItems.SelectedItem;
            if (item != null)
            {
                ItemAdd.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
                ItemAdd.StaysOpen = true;
                ItemAdd.IsOpen = true;
                resetPopup(item);
                btnClose.IsEnabled = false;
                itemList.Remove(item);
            }
        }

        private void resetPopup(Item item)
        {
            txtQuantity.Text = item.Quantity.ToString();
            txtProductName.Text = item.Name;
            txtDescription.Text = item.Description;
            cmbCategory.SelectedValue = item.Category;
            txtAdress.Text = item.StoreLocation.Address;
            txtCity.Text = item.StoreLocation.City;
            txtCost.Text = item.Cost.ToString();
            txtState.Text = item.StoreLocation.State;
            txtPostalCode.Text = item.StoreLocation.Zip;
            txtStoreName.Text = item.StoreLocation.Name;
            btnClose.IsEnabled = true;
        }
    }
}
